#include "Col.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#define FakeExternalDecl
#include "Point.h"

#include "Class.h"
#include "Math.h"
#include "String.h"
#include "ET_stdio.h"

SimpleMetaImpl(Point)
{
	len= len;
	if (! isptr)
		sprintf(buf, "%s", ((Point*) addr)->AsString());
}

Point Min(const Point &p1, const Point &p2)
{
	return Point(Math::Min(p1.x, p2.x), Math::Min(p1.y, p2.y));
}

Point Max(const Point &p1, const Point &p2)
{
	return Point(Math::Max(p1.x, p2.x), Math::Max(p1.y, p2.y));
}

Point Range (const Point &lb, const Point &ub, const Point &x)
{
	return Point(Math::Range(lb.x, ub.x, x.x), Math::Range(lb.y, ub.y, x.y));
}

Point Point::Half() const
{
	return Point(x >> 1, y >> 1);
}

Point Half (const Point &p)
{
	return Point(p.x/2, p.y/2);
}

Point Abs(const Point &p)
{
	return Point(Math::Abs(p.x), Math::Abs(p.y));
}

Point Sign(const Point &p)
{
	return Point(Math::Sign(p.x), Math::Sign(p.y));
}

Point Scale(const Point &p, const Point &num, const Point &denom)
{
	Point r;

	if (denom.x)
		r.x= (int)((float)(p.x*num.x) / (float)denom.x + 0.5);
	if (denom.y)
		r.y= (int)((float)(p.y*num.y) / (float)denom.y + 0.5);
		
	return r;
}

Point Scale(const Point &p, float sx, float sy)
{
	return Point((float)((p.x*sx)+0.5), (float)((p.y*sy)+0.5));
}

float Phi(const Point &p)
{
	return Math::Atan2(p.y, p.x);
}

float Length(const Point &p)
{
	return (float) Math::Hypot(p.x, p.y);
}

Point PolarToPoint(double ang, double fx, double fy)
{
	double si, co;
	Math::Sincos(ang, &si, &co);
	return Point((int)(fx*co+0.5), (int)(fy*si+0.5));
}

Point polartopoint(double ang, double fx, double fy)
{
	double si, co;
	Math::Sincos(ang, &si, &co);
	return Point((int)(fx*co+0.5), (int)(fy*si+0.5));
}

void Swap(Point &p1, Point &p2)
{
	Point tmp= p1;
	p1= p2;
	p2= tmp;
}

Point Flip(const Point &p)
{
	return Point(p.y, p.x);
}

OStream& operator<< (OStream &os, const Point &p)
{
	return os << p.x SP << p.y SP;
}

IStream& operator>> (IStream &is, Point &p)
{
	return is >> p.x >> p.y;
}

const char *Point::AsString() const
{
	return form("x: %d y: %d", x, y);
}

